package test.administrator.example.com.mvptest1.framework.support.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.framework.support.activity.ActivityMvpDelegate;
import test.administrator.example.com.mvptest1.framework.support.activity.ActivityMvpDelegateImpl;
import test.administrator.example.com.mvptest1.framework.support.MvpDelegateCallback;

/**
 * 中介者模式->具体同事A（抽象）->绑定V层和解绑V层
 * 代理模式->第一重代理->代理对象->生命周期（持有目标对象引用）->MvpActivity
 * 代理模式->第二重代理->目标对象->绑定和解绑->MvpActivity(实现目标接口)
 * Created by thinkpad on 2018/7/17.
 */
public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>> extends Activity implements MvpView, MvpDelegateCallback<V, P> {

	//目标对象
	protected ActivityMvpDelegate<V, P> mvpDelegate;

	protected ActivityMvpDelegate<V, P> getMvpDelegate() {
		if (this.mvpDelegate == null) {
			this.mvpDelegate = new ActivityMvpDelegateImpl<V, P>(this, this, true);
		}
		return mvpDelegate;
	}

	/****第二重代理****/
	private P presenter;

	@Override
	public P createPresenter() {
		return presenter;
	}

	@Override
	public P getPresenter() {
		return presenter;
	}

	@Override
	public void setPresenter(P presenter) {
		this.presenter = presenter;
	}

	@Override
	public V getMvpView() {
		return (V) this;
	}

	/****结束****/

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getMvpDelegate().onCreate(savedInstanceState);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getMvpDelegate().onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		getMvpDelegate().onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		getMvpDelegate().onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		getMvpDelegate().onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
		getMvpDelegate().onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getMvpDelegate().onDestroy();
	}

}

































































































